package Eve.EveB;

import java.util.TreeMap;
import java.util.Iterator;
/**
 * A data structure to keep dictionary words by it's length.
 * The d.s. is sorted by the priority of each word,
 * every length group is sorted seperatly.
 * Adding an exist word increases it's priority. 
 */
public class SuperDic {
    private TreeMap<Integer, Dictionary> m_lengthRBT;
    
    public SuperDic(){
        m_lengthRBT = new TreeMap<Integer, Dictionary>();
    }

    public void put(String str){
        Integer len = str.length();
        Dictionary dic;
        if(m_lengthRBT.containsKey(len)){
            dic = m_lengthRBT.get(len);
        }
        else{
            dic = new Dictionary();
        }
        dic.put(str);
        m_lengthRBT.put(len, dic);
    }

    public void remove(String str){
        Integer len = str.length();
        if(m_lengthRBT.containsKey(len)){
            Dictionary dic = m_lengthRBT.get(len);
            dic.remove(str);
        }
    }

    public Iterator getIterator(int len){
        if(m_lengthRBT.containsKey(len)){
            return m_lengthRBT.get(len).iterator();
        }

        return new Iterator(){

            public boolean hasNext() {
                return false;
            }

            public Object next() {
                return null;
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

}
